#include "InputHandler.h"
#include "GameGlobals.h"

sf::Vector2f InputHandler::LKPMouse = sf::Vector2f(0.f,0.f);

bool InputHandler::IsKeyPressed(sf::Keyboard::Key Key)
{
	if (!GameGlobals::Instance()->InFocus)
		return false;
	return sf::Keyboard::IsKeyPressed(Key);
}

bool InputHandler::IsMouseButtonPressed(sf::Mouse::Button Button)
{
	if (!GameGlobals::Instance()->InFocus)
		return false;
	return sf::Mouse::IsButtonPressed(Button);
}

sf::Vector2f InputHandler::GetMousePosWorld(sf::RenderWindow &App)
{
	sf::Vector2f WindPos = GetMousePos(App);
	/*WindPos = WindPos - App.GetView().GetCenter();
	WindPos.y *= -1;

	return WindPos;*/

	sf::Vector2<double> dPos;
	dPos.x = WindPos.x;
	dPos.y = WindPos.y;

	sf::Vector2<double> ScreenCentre = sf::Vector2<double>(App.GetWidth() / 2, App.GetHeight() / 2);
	sf::Vector2<double> ViewCentre;
	ViewCentre.x = App.GetView().GetCenter().x;
	ViewCentre.y = App.GetView().GetCenter().y;

	dPos = dPos - ViewCentre - (ScreenCentre - ViewCentre);
	dPos.y *= -1;

	WindPos.x = dPos.x;
	WindPos.y = dPos.y;

	return WindPos;
}

sf::Vector2f InputHandler::GetMousePos(sf::RenderWindow &App)
{
	if (!GameGlobals::Instance()->InFocus)
		return LKPMouse;
	sf::Vector2f Pos;
	sf::Vector2i iPos = sf::Mouse::GetPosition(App);
	Pos.x = float(iPos.x);
	Pos.y = float(iPos.y);
	Pos = App.ConvertCoords(Pos.x, Pos.y);
	LKPMouse = Pos;

	return Pos;
}
